//   @Name    : merchant_driver
//   @author  : 海参
//   @date    : 2022/4/13 19:01 AM
//   @desc    :
//   2024-08-20 图片展示方式修改
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class CaiImage {
  ///Image.asset：本地image,fit默认BoxFit.contain:
  ///BoxFit.contain:尽可能大，同时仍然将源完全包含在目标框中。内容完全显示，会以最大边缩小显示。
  ///BoxFit.cover:尽可能小，同时仍然覆盖整个目标框。内容不完全显示，会以最小边放大显示，其余裁剪,填充满整个box。一般用于网络图片。
  ///scaleDown:原始图片居中显示。
  static Widget assetImage({
    required String name,
    double? width,
    double? height,
    double? size,
    Color? color,
    BoxFit? fit,
  }) {
    return Image.asset('assets/images/$name.png',
        width: width ?? size,
        height: height ?? size,
        color: color,
        fit: fit ?? BoxFit.contain,
        errorBuilder: (
          BuildContext context,
          Object error,
          StackTrace? stackTrace,
        ) =>
            const Icon(Icons.error));
  }

  ///Image.network：网络图片,改用networkFadeInImage，基本不用
  static Widget networkImage({
    required String url,
    double? width,
    double? height,
    BoxFit? fit,
    ImageLoadingBuilder? loadingBuilder,
    ImageErrorWidgetBuilder? errorBuilder,
  }) {
    return Image.network(
      url,
      width: width,
      height: height,
      fit: fit ?? BoxFit.cover,
      loadingBuilder: loadingBuilder ??
          (
            BuildContext context,
            Widget child,
            ImageChunkEvent? loadingProgress,
          ) {
            return Container(
              child: child,
            );
          },
      errorBuilder: errorBuilder ??
          (
            BuildContext context,
            Object error,
            StackTrace? stackTrace,
          ) {
            return const Icon(Icons.error);
          },
    );
  }

  ///渐变过渡显示网络图片:
  ///placeholder:本地资源占位图完整路径，
  ///placeholderImageName：本地资源占位图资源名，自动拼接上路径。placeholderImageName必须在assets/images/目录下；
  ///placeholder和placeholderImageName只能选一个。
  ///注意：FadeInImage.assetNetwork方法有bug。如果url一开始是无效的，后面请求完设置有效url，再setState，widget就会变成空白。
  ///这种本地数据更新网络数据的场景，需要使用cached_network_image库才行。
  static Widget networkFadeInImage(
      {required String url,
      String? placeholder,
      String? placeholderImageName,
      double? width,
      double? height,
      BoxFit? fit,
      ImageErrorWidgetBuilder? imageErrorBuilder,
      ImageErrorWidgetBuilder? placeholderErrorBuilder}) {
    String placeholder1 = placeholder ??
        'assets/images/${placeholderImageName ?? 'ic_placeholder'}.png';
    return FadeInImage.assetNetwork(
      placeholder: placeholder1,
      image: url,
      width: width,
      height: height,
      fit: fit ?? BoxFit.cover,
      placeholderErrorBuilder: placeholderErrorBuilder ??
          (
            BuildContext context,
            Object error,
            StackTrace? stackTrace,
          ) {
            return const Icon(
              Icons.error,
              color: Colors.red,
            );
          },
      imageErrorBuilder: imageErrorBuilder ??
          (
            BuildContext context,
            Object error,
            StackTrace? stackTrace,
          ) {
            return CaiImage.assetImage(
                name: 'ic_placeholderError', width: width, height: height);
          },
    );
  }

  ///第三方库的网路图片加载;fit默认是BoxFit.contain
  static Widget cachedNetworkImage({
    required String imageUrl,
    double? width = 80,
    double? height = 80,
    BoxFit? fit,
    PlaceholderWidgetBuilder? placeholder,
  }) {
    return CachedNetworkImage(
      imageUrl: imageUrl,
      width: width,
      height: height,
      fit: fit,
      placeholder: placeholder ??
          (context, url) => CaiImage.assetImage(
              name: 'ic_placeholder', width: width, height: height),
      errorWidget: (context, url, error) {
        return CaiImage.assetImage(
            name: 'ic_placeholderError', width: width, height: height);
      },
    );
  }
}
